package cn.iocoder.yudao.module.jky.controller.admin.goods;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.jky.controller.admin.goods.vo.*;
import cn.iocoder.yudao.module.jky.controller.admin.goodsattribute.vo.GoodsAttributeRespVO;
import cn.iocoder.yudao.module.jky.controller.admin.goodscat.vo.GoodsCatExportReqVO;
import cn.iocoder.yudao.module.jky.controller.admin.goodscat.vo.GoodsCatRespVO;
import cn.iocoder.yudao.module.jky.convert.goods.GoodsConvert;
import cn.iocoder.yudao.module.jky.convert.goodsattribute.GoodsAttributeConvert;
import cn.iocoder.yudao.module.jky.convert.goodsattributevalue.GoodsAttributeValueConvert;
import cn.iocoder.yudao.module.jky.convert.goodscat.GoodsCatConvert;
import cn.iocoder.yudao.module.jky.dal.dataobject.goods.GoodsDO;
import cn.iocoder.yudao.module.jky.dal.dataobject.goodsattribute.GoodsAttributeDO;
import cn.iocoder.yudao.module.jky.dal.dataobject.goodsattributevalue.GoodsAttributeValueDO;
import cn.iocoder.yudao.module.jky.dal.dataobject.goodscat.GoodsCatDO;
import cn.iocoder.yudao.module.jky.service.goods.GoodsService;
import cn.iocoder.yudao.module.jky.service.goodsattribute.GoodsAttributeService;
import cn.iocoder.yudao.module.jky.service.goodsattributevalue.GoodsAttributeValueService;
import cn.iocoder.yudao.module.jky.service.goodsaudit.GoodsAuditService;
import cn.iocoder.yudao.module.jky.service.goodscat.GoodsCatService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台供应商 - 商品")
@RestController
@RequestMapping("/supplier/goods-audit")
@Validated
public class GoodsAuditController {

    @Resource
    private GoodsService goodsService;

    @Resource
    private GoodsAuditService goodsAuditService;

    @Resource
    private GoodsCatService goodsCatService;

    @Resource
    private GoodsAttributeService goodsAttributeService;

    @Resource
    private GoodsAttributeValueService goodsAttributeValueService;


    @GetMapping("/get")
    @Operation(summary = "获得商品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('jky:goods-audit:query')")
    public CommonResult<GoodsRespVO> getGoods(@RequestParam("id") Integer id) {
        GoodsDO goods = goodsService.getGoods(id);
        GoodsRespVO respVO = GoodsConvert.INSTANCE.convert(goods);
        respVO.setSkuStockList(goodsService.getSkuList(id));
        return success(respVO);
    }


    @GetMapping("/page")
    @Operation(summary = "获得商品分页")
    @PreAuthorize("@ss.hasPermission('jky:goods-audit:query')")
    public CommonResult<PageResult<GoodsRespVO>> getGoodsPage(@Valid GoodsPageReqVO pageVO) {
        PageResult<GoodsDO> pageResult = goodsService.getGoodsPage(pageVO);
        return success(GoodsConvert.INSTANCE.convertPage(pageResult));
    }

    @PutMapping("/audit")
    @Operation(summary = "审核商品")
    @PreAuthorize("@ss.hasPermission('jky:goods-audit:update')")
    public CommonResult<Boolean> auditGoods(@Valid @RequestBody GoodsAuditReqVO reqVO) {
        goodsService.auditGoods(reqVO);
        return success(Boolean.TRUE);
    }

    @GetMapping("/cat-list")
    @Operation(summary = "获得商品分类列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('jky:goods-audit:cat')")
    public CommonResult<List<GoodsCatRespVO>> getGoodsCatList(GoodsCatExportReqVO exportReqVO) {
        List<GoodsCatDO> list = goodsCatService.getGoodsCatList(exportReqVO);
        return success(GoodsCatConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/attr-list")
    @Operation(summary = "获得商品属性列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('jky:goods-attribute:attr')")
    public CommonResult<List<GoodsAttributeRespVO>> getGoodsAttributeList(@RequestParam("id") Short catId) {
        List<GoodsAttributeDO> list = goodsAttributeService.getGoodsAttributeList(catId);
        list.forEach(item -> {
            List<GoodsAttributeValueDO> goodsAttributeValueList = goodsAttributeValueService.getGoodsAttributeValueList(item.getGoodsAttributeId());
            item.setValueList(GoodsAttributeValueConvert.INSTANCE.convertList(goodsAttributeValueList));
        });
        return success(GoodsAttributeConvert.INSTANCE.convertList(list));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出商品 Excel")
    @PreAuthorize("@ss.hasPermission('jky:goods-audit:export')")
    @OperateLog(type = EXPORT)
    public void exportGoodsExcel(@Valid GoodsExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<GoodsDO> list = goodsService.getGoodsList(exportReqVO);
        // 导出 Excel
        List<GoodsExcelVO> datas = GoodsConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "商品.xls", "数据", GoodsExcelVO.class, datas);
    }

}
